package com.ikang.myapplication;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.widget.RelativeLayout;
import java.util.ArrayList;

public class RippleBackground extends RelativeLayout {
    private static final int DEFAULT_RIPPLE_COUNT = 1;
    private static final int DEFAULT_DURATION = 1500;
    private static final float DEFAULT_SCALE = 1.4f;
    private static final int DEFAULT_FILL_COLOR = Color.parseColor("#FF00B4FF");

    private float rippleRadius;
    private int rippleDuration;
    private int rippleAmount;
    private float rippleScale;
    private int rippleColor;
    private Paint paint;
    private boolean animationRunning = false;
    private boolean animationPaused = false;
    private AnimatorSet animatorSet;
    private ArrayList<RippleView> rippleViewList = new ArrayList<>();

    public RippleBackground(Context context) {
        super(context);
        init(context, null);
    }

    public RippleBackground(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        if (isInEditMode())
            return;

        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.RippleBackground);
        rippleColor = typedArray.getColor(R.styleable.RippleBackground_rb_color, DEFAULT_FILL_COLOR);
        rippleRadius = typedArray.getDimension(R.styleable.RippleBackground_rb_radius, 32);
        rippleDuration = typedArray.getInt(R.styleable.RippleBackground_rb_duration, DEFAULT_DURATION);
        rippleAmount = typedArray.getInt(R.styleable.RippleBackground_rb_rippleAmount, DEFAULT_RIPPLE_COUNT);
        rippleScale = typedArray.getFloat(R.styleable.RippleBackground_rb_scale, DEFAULT_SCALE);
        typedArray.recycle();

        paint = new Paint();
        paint.setAntiAlias(true);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(context.getResources().getDisplayMetrics().density);  // 设置为2dp
        paint.setColor(rippleColor);

        for (int i = 0; i < rippleAmount; i++) {
            RippleView rippleView = new RippleView(getContext());
            addView(rippleView, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
            rippleViewList.add(rippleView);
        }
    }

    public void startRippleAnimation() {
        if (!animationRunning || animationPaused) {
            animationRunning = true;
            animationPaused = false;
            
            if (animatorSet != null) {
                animatorSet.cancel();
            }
            
            animatorSet = new AnimatorSet();
            ArrayList<Animator> animatorList = new ArrayList<>();

            for (int i = 0; i < rippleAmount; i++) {
                RippleView rippleView = rippleViewList.get(i);
                rippleView.setVisibility(VISIBLE);
                float delay = i * rippleDuration / rippleAmount;

                ObjectAnimator scaleXAnimator = ObjectAnimator.ofFloat(rippleView, "scaleX", 1.0f, rippleScale);
                scaleXAnimator.setRepeatCount(ObjectAnimator.INFINITE);
                scaleXAnimator.setRepeatMode(ObjectAnimator.RESTART);
                scaleXAnimator.setStartDelay((long) delay);
                scaleXAnimator.setDuration(rippleDuration);
                animatorList.add(scaleXAnimator);

                ObjectAnimator scaleYAnimator = ObjectAnimator.ofFloat(rippleView, "scaleY", 1.0f, rippleScale);
                scaleYAnimator.setRepeatCount(ObjectAnimator.INFINITE);
                scaleYAnimator.setRepeatMode(ObjectAnimator.RESTART);
                scaleYAnimator.setStartDelay((long) delay);
                scaleYAnimator.setDuration(rippleDuration);
                animatorList.add(scaleYAnimator);

                ObjectAnimator alphaAnimator = ObjectAnimator.ofFloat(rippleView, "alpha", 1.0f, 0.3f);
                alphaAnimator.setRepeatCount(ObjectAnimator.INFINITE);
                alphaAnimator.setRepeatMode(ObjectAnimator.RESTART);
                alphaAnimator.setStartDelay((long) delay);
                alphaAnimator.setDuration(rippleDuration);
                animatorList.add(alphaAnimator);
            }

            animatorSet.setInterpolator(new AccelerateDecelerateInterpolator());
            animatorSet.playTogether(animatorList);
            animatorSet.start();
        } else if (animationPaused) {
            resumeRippleAnimation();
        }
    }

    public void stopRippleAnimation() {
        if (animationRunning) {
            animationRunning = false;
            animationPaused = false;
            if (animatorSet != null) {
                animatorSet.end();
                for (RippleView rippleView : rippleViewList) {
                    rippleView.setVisibility(INVISIBLE);
                }
            }
        }
    }

    public boolean isRippleAnimationRunning() {
        return animationRunning && !animationPaused;
    }

    public void pauseRippleAnimation() {
        if (animationRunning && !animationPaused) {
            animationPaused = true;
            if (animatorSet != null) {
                animatorSet.pause();
            }
        }
    }

    public void resumeRippleAnimation() {
        if (animationRunning && animationPaused) {
            animationPaused = false;
            if (animatorSet != null) {
                animatorSet.resume();
            }
        }
    }

 private class RippleView extends View {
     public RippleView(Context context) {
         super(context);
         this.setVisibility(INVISIBLE);
     }
 
     @Override
     protected void onDraw(Canvas canvas) {
         int width = getWidth();
         int height = getHeight();
         int radius = Math.min(width, height) / 4;  
         
         // 绘制圆形边框
         canvas.drawCircle(width / 2, height / 2, radius, paint);
     }
 }
}